Binding SQL parameters with Java standard object
https://gyazo.com/f2458460d144fd444ac74cc6a359f1ff
For example, in the following select query, Alice is set by PreparedStatement#setString , 30 is set by PreparedStatement#setInt.
code:select.java
List<Customer> customers = sorm.readList(Customer.class, "SELECT * FROM customer WHERE name=? AND age=?","Alice",30);
/yuunkjm/--.icon
Create parameterized SQL statement
Sorm4j basically uses simply ordered parameters. So you can simply call as the follows: code:java
List<Customer> customers = sorm.readList(Customer.class, "select * from players where id=? and name=?", 1, "Alice");
If you want to split creating SQL statement and executing it, you could use OrderedParameterSql, NamedParameterSql and ParameterizedSql.
Ordered Parameter
code:java
String sql = "select * from players where id=? and name=?";
ParameterizedSql psql = ParameterizedSql.of(sql, 1, "Alice")
// or
ParameterizedSql psql = OrderedParameterSql.of(sql).addParameter(1, "Alice").parse();
List<Customer> customers = sorm.readList(Customer.class, psql);
Named Parameter
code:java
String sql = "select * from customer where id=:id and address=:address";
ParameterizedSql statement = NamedParameterSql.parse(sql, Map.of("id",1, "address","Kyoto"));
//or
ParameterizedSql statement =
NamedParameterSql.of(sql).bind("id", 1).bind("address", "Kyoto").parse();
List<Customer> customers = sorm.readList(Customer.class, statement);
/yuunkjm/--.icon
List parameter & Embedded Parameter
List parameter <?> is converted to a sequence of ? joined comma depending on the given list size. You can bind multiple values at once by <?> and a List<T>.
This is mainly designed for IN clause
Embedded Parameter {?} is embedded parameter. it is embedded to SQL statement before executing prepared statement.
code:example.java
OrderedParameterSqlParser.parse(
"select * from customer where name like {?} and address in(<?>) and id=?", "'A%'", List.of("Tokyo", "Kyoto"), 1);
//or
NamedParameterSqlParser.parse(
"select * from customer where name like {:name} and address in(<:address>) and id=:id",
Map.of("id", 1, "address", List.of("Tokyo", "Kyoto"), "name", "A%"));
is converted to:
sql=[select * from customer where name like 'A%' and address in(?,?) and id=?], parameters=[Tokyo, Kyoto, 1]